package com.btkjgc.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.extend.ConventionalPostageModel;
import com.btkjgc.service.intf.ConventionalFareManageService_Intf;

@Controller
public class ConventionaFareManageController
{
	static Logger logger = Logger.getLogger(ConventionaFareManageController.class);
	
	@Autowired
	private ConventionalFareManageService_Intf conventionalPostageManageService;
	
	@RequestMapping(value = "conventional_fare_manage", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request,ConventionalPostageModel conventionalPostageModel) 
	{
		ModelAndView mv = new ModelAndView("conventional_fare_manage");
		listrecord(request,mv,conventionalPostageModel);
		return mv;
	}
	
	@RequestMapping(value = "view_conventional_fare", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView addConventionalFare(HttpServletRequest request,ConventionalPostageModel conventionalPostageModel)
	{
		 ModelAndView mv = new ModelAndView("add_conventional_fare");
		 if ("edit".equals(conventionalPostageModel.getTask())) {
			 conventionalPostageManageService.setEditdata(request,mv,conventionalPostageModel.getId());
		 }
		return mv;
	}

	@SuppressWarnings("unchecked")
	private void listrecord(HttpServletRequest request, ModelAndView mv,ConventionalPostageModel conventionalPostageModel) 
	{
 
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if(StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}
		
		int pageIndex = 0;
		int totalRecord = 0;
		if (page != null)
		{
			pageIndex = Integer.parseInt(page);
		}
		
		List<Object[]> paramList = new ArrayList<Object[]>();
		String[] arr = { "name","like", conventionalPostageModel.getName()};
		paramList.add(arr);
 
		
		String sql = "SELECT\n" +
				"cpt.*, aut.username,\n" +
				"case \n" +
				"when cpt.endTime is null then '不限'\n" +
				"ELSE cpt.endTime\n" +
				"end showEndTime,\n" +
				"case  \n" +
				"when cpt.beginTime is null then '即时生效'\n" +
				"ELSE   cpt.beginTime\n" +
				"end showBeginTime,\n" +
				"case type\n" +
				"when 1  then '里程'\n" +
				"when 2  then '次数'\n" +
				"end typeName,\n" +
				"case type\n" +
				"when 1  then '元/公里'\n" +
				"when 2  then '元/次'\n" +
				"end unitName,\n" +
				"case status\n" +
				"when 1  then '开启'\n" +
				"when 2  then '暂停'\n" +
				"end statusName FROM\n" +
				"	conventional_postage_t cpt\n" +
				"LEFT JOIN admin_user_t aut ON aut.id = cpt.operid\n" +
				"WHERE\n" +
				"	cpt.del = 0 ";
		
		sql = conventionalPostageManageService.getwhereSQL(sql, paramList, sortfield);
		
		totalRecord = conventionalPostageManageService.getCount(sql, paramList);
		
		// 获取分页查询的数据结果
		List<ConventionalPostageModel> conventionalPostageList = conventionalPostageManageService.getList(
						ConventionalPostageModel.class, paramList, sql, pageIndex, 15, sortfield);
		
		// 设置后台用户界面所需的其他数据
		conventionalPostageManageService.setListData(request, mv, pageIndex, totalRecord,
				conventionalPostageList);
		
	}
	
	@RequestMapping(value = "batchdel_conventional_fare", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String batchdelConventionalFare(HttpServletRequest request, String idStr) {
		
		conventionalPostageManageService.updateDelByIds(idStr);
		
		return "redirect:/conventional_fare_manage";
	}
	
	@RequestMapping(value = "batchpause_conventional_fare", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String batchpauseConventionalFare(HttpServletRequest request, String idStr) {

		conventionalPostageManageService.updateStateByIds(idStr,2);
		
		return "redirect:/conventional_fare_manage";
	}
	
	@RequestMapping(value = "batchstart_conventional_fare", method = {
			RequestMethod.GET, RequestMethod.POST })
	public String batchstartConventionalFare(HttpServletRequest request, String idStr) {
		
		conventionalPostageManageService.updateStateByIds(idStr,1);
		
		return "redirect:/conventional_fare_manage";
	}
	
	
	@RequestMapping(value = "edit_conventional_fare", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public AjaxObj editPackFare(ConventionalPostageModel conventionalPostageModel)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {

		AjaxObj ajaxObj=conventionalPostageManageService.updateConventionalPostageModel(conventionalPostageModel);

		return ajaxObj;
	}
	
	@RequestMapping(value = "add_conventional_fare/{a}")
	@ResponseBody
	public   AjaxObj jsonAddConventionalFare(HttpServletRequest request,
			@PathVariable("a") int a ,ConventionalPostageModel conventionalPostageModel) {
 
 
		  AjaxObj ajaxObj = conventionalPostageManageService
					.insertConventionalPostageModel(conventionalPostageModel);
 
		return ajaxObj;
	}
	
	
}